package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.EmployeeMapper;
import com.hqyj.pojo.Employee;
import com.hqyj.pojo.Setting;
import com.hqyj.service.EmployeeService;
import com.hqyj.util.MdFive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /*
    查询所有签到记录
     */
    /*public HashMap<String, Object> selectByPrimaryKeySigninSignout(Employee e) {
        HashMap<String, Object> map =new HashMap<String, Object>();

        return employeeMapper.selectByPrimaryKeySigninSignout(e);
    }*/
        /*
         查询单条记录
        */
    public HashMap<String, Object> selectByEnumber(int eNumber) {
        HashMap<String, Object> map =new HashMap<String, Object>();

        List<Employee> list = employeeMapper.selectByEnumber(eNumber);
        if(list.size()>0){
            map.put("info",list);
        }else{
            map.put("info","0");
        }
        return map;
    }
        /*
        分页模糊查询e.getDepa()!=null
         */
    public HashMap<String, Object> selectBy( Employee e) {
        HashMap<String, Object> map =new HashMap<String, Object>();
        //模糊查询判断
        if(e.getName()!=null && e.getName().equals(" ")){
            e.setName("%"+e.getName()+"%");
        }
        if(e.getDepa()!=null && e.getDepa().equals(" ")){
            e.setDepa("%"+e.getDepa()+"%");
        }
        //分页
        //1 设置分页查询的页码，每页显示的行数
        PageHelper.startPage(e.getPage(), e.getRow());
        //2 查询
        List<Employee> list =employeeMapper.selectBy(e);
       /* if(list.size()>0){
            map.put("info",list);
        }else{
            map.put("info","0");
        }*/
        //3 获取分页对象
        PageInfo<Employee> pageInfo =new PageInfo<Employee>(list);

        if(list.size()==0){
            map.put("info","没有查询到数据");
        }else{
            //获取当前集合
            map.put("info",pageInfo.getList());
            //总条数
            Long  total = pageInfo.getTotal();
            //总页数
            int  totalPage  = pageInfo.getPages();
            System.out.println("上一页="+pageInfo.getPrePage());
            System.out.println("下一页="+pageInfo.getNextPage());
            map.put("total",total);
            map.put("totalPage",totalPage);
        }
        return map;

    }


    @Autowired
    MdFive mdFive;
    public HashMap<String, Object> selectByENumber(Employee employee) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            //1分页 设置分页页码，每页显示条数
            PageHelper.startPage(employee.getPage(), employee.getRow());
            //查询
            List<Employee> employees = employeeMapper.selectList(employee);
            //获取分页对象
            PageInfo<Employee> jobPageInfo = new PageInfo<Employee>(employees);

            if (employees != null || employees.size() > 1) {

                map.put("info", employees);
                //总条数
                long total = jobPageInfo.getTotal();
                //总页数
                int pages = jobPageInfo.getPages();

                System.out.println("上一页=" + jobPageInfo.getPrePage());
                System.out.println("下一页=" + jobPageInfo.getNextPage());
                map.put("total", total);
                map.put("pages", pages);
            } else {
                map.put("info", "没有查询到信息");
                System.out.println("没有查询到信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //添加员工信息
    public HashMap<String, Object> addEmployee(Employee employee) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        try {
            Object encrypt = mdFive.encrypt(employee.getePwd(), employee.geteName());
            employee.setePwd(encrypt.toString());

            //查询是否有重复的用户名
            String s = employeeMapper.SelectEnameByEname(employee.geteName());

            if (s != null) {
                map.put("info", "用户名重复，请重新输入！");
                return map;
            }
            int i = employeeMapper.insertSelective(employee);
            if (i > 0) {
                map.put("info", "添加成功！");
            } else {
                map.put("info", "添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //删除员工

    public HashMap<String, Object> deleteByID(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = employeeMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            map.put("info", "删除成功！");
        } else {
            map.put("info", "删除失败！");
        }
        return map;
    }

    //修改密码
    public HashMap<String, Object> updatePwd(Employee employee) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        Object encrypt = mdFive.encrypt(employee.getePwd(), employee.geteName());


        employee.setePwd(encrypt.toString());


        int i = employeeMapper.updateByPrimaryKeySelective(employee);
        if (i > 0) {
            map.put("info", "更改成功,请重新登录！");
        } else {
            map.put("info", "更改失败！");
        }
        return map;
    }
}
